package com.kexio.security.domain.enums;

/**
 * 认证失败原因枚举
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public enum AuthFailureReason {
    /**
     * 用户不存在
     */
    USER_NOT_EXISTS("USER_NOT_EXISTS", "用户不存在"),
    
    /**
     * 密码错误
     */
    INVALID_PASSWORD("INVALID_PASSWORD", "密码错误"),
    
    /**
     * 用户被禁用
     */
    USER_DISABLED("USER_DISABLED", "用户被禁用"),
    
    /**
     * 用户被锁定
     */
    USER_LOCKED("USER_LOCKED", "用户被锁定"),
    
    /**
     * 密码已过期
     */
    PASSWORD_EXPIRED("PASSWORD_EXPIRED", "密码已过期"),
    
    /**
     * 账户已过期
     */
    ACCOUNT_EXPIRED("ACCOUNT_EXPIRED", "账户已过期"),
    
    /**
     * 验证码错误
     */
    INVALID_CAPTCHA("INVALID_CAPTCHA", "验证码错误"),
    
    /**
     * 验证码已过期
     */
    CAPTCHA_EXPIRED("CAPTCHA_EXPIRED", "验证码已过期"),
    
    /**
     * 登录次数过多
     */
    TOO_MANY_ATTEMPTS("TOO_MANY_ATTEMPTS", "登录次数过多"),
    
    /**
     * 令牌无效
     */
    INVALID_TOKEN("INVALID_TOKEN", "令牌无效"),
    
    /**
     * 令牌已过期
     */
    TOKEN_EXPIRED("TOKEN_EXPIRED", "令牌已过期"),
    
    /**
     * 权限不足
     */
    INSUFFICIENT_PERMISSIONS("INSUFFICIENT_PERMISSIONS", "权限不足"),
    
    /**
     * 会话已过期
     */
    SESSION_EXPIRED("SESSION_EXPIRED", "会话已过期"),
    
    /**
     * IP地址受限
     */
    IP_RESTRICTED("IP_RESTRICTED", "IP地址受限"),
    
    /**
     * 时间范围受限
     */
    TIME_RESTRICTED("TIME_RESTRICTED", "时间范围受限"),
    
    /**
     * 设备未授权
     */
    DEVICE_UNAUTHORIZED("DEVICE_UNAUTHORIZED", "设备未授权"),
    
    /**
     * 多地登录受限
     */
    MULTI_LOCATION_RESTRICTED("MULTI_LOCATION_RESTRICTED", "多地登录受限"),
    
    /**
     * 系统错误
     */
    SYSTEM_ERROR("SYSTEM_ERROR", "系统错误"),
    
    /**
     * 未知错误
     */
    UNKNOWN("UNKNOWN", "未知错误");

    private final String code;
    private final String description;

    AuthFailureReason(String code, String description) {
        this.code = code;
        this.description = description;
    }

    public String getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 根据编码获取失败原因
     * @param code 失败原因编码
     * @return 失败原因枚举
     */
    public static AuthFailureReason fromCode(String code) {
        if (code == null) {
            return null;
        }
        for (AuthFailureReason reason : values()) {
            if (reason.getCode().equals(code)) {
                return reason;
            }
        }
        return UNKNOWN;
    }

    /**
     * 检查是否为用户相关错误
     * @return 是否用户相关错误
     */
    public boolean isUserRelated() {
        return this == USER_NOT_EXISTS 
            || this == USER_DISABLED 
            || this == USER_LOCKED 
            || this == ACCOUNT_EXPIRED;
    }

    /**
     * 检查是否为密码相关错误
     * @return 是否密码相关错误
     */
    public boolean isPasswordRelated() {
        return this == INVALID_PASSWORD 
            || this == PASSWORD_EXPIRED;
    }

    /**
     * 检查是否为验证码相关错误
     * @return 是否验证码相关错误
     */
    public boolean isCaptchaRelated() {
        return this == INVALID_CAPTCHA 
            || this == CAPTCHA_EXPIRED;
    }

    /**
     * 检查是否为令牌相关错误
     * @return 是否令牌相关错误
     */
    public boolean isTokenRelated() {
        return this == INVALID_TOKEN 
            || this == TOKEN_EXPIRED;
    }

    /**
     * 检查是否为安全策略相关错误
     * @return 是否安全策略相关错误
     */
    public boolean isSecurityPolicyRelated() {
        return this == TOO_MANY_ATTEMPTS 
            || this == IP_RESTRICTED 
            || this == TIME_RESTRICTED 
            || this == DEVICE_UNAUTHORIZED 
            || this == MULTI_LOCATION_RESTRICTED;
    }

    /**
     * 检查是否需要用户重新登录
     * @return 是否需要重新登录
     */
    public boolean requiresReLogin() {
        return this == SESSION_EXPIRED 
            || this == TOKEN_EXPIRED 
            || this == PASSWORD_EXPIRED;
    }
}